<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="76px">
        <el-form-item label="处理状态">
          <el-select
            v-model="searchParam.repairStatus"
            placeholder="请选择处理状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamRepairStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="维修班组">
          <el-select
            v-model="searchParam.repairTeamId"
            placeholder="请输入维修班组"
            clearable
            filterable
          >
            <el-option
              v-for="item in crewList"
              :key="item.id"
              :label="`${item.teamName}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="故障状态">
          <el-select
            style="width: 100%"
            v-model="searchParam.faultStatus"
            placeholder="请选择故障状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamFaultStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="故障来源">
          <el-select
            style="width: 100%"
            v-model="searchParam.repairSource"
            placeholder="请选择故障来源"
            clearable
          >
            <el-option
              v-for="item in useDictionary('EamFaultSource')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <div v-if="isExpanded" style="display: inline">
          <!-- <el-form-item label="使用部门">
            <el-select
              style="width: 100%"
              v-model="searchParam.usingDepartmentName"
              placeholder="请选择使用部门"
              clearable
            >
              <el-option
                v-for="item in useDictionary('EamFaultSource')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="使用部门">
            <SelectOriganizationDangerGrant
              @change="(ids:string) => getSelectIds(ids)"
              :isCheckStrictly="true"
              :isMultiple="false"
              :disabled="false"
              :data="dataObject('使用部门')"
              :selectData="searchParam.usingDepartmentId"
            />
          </el-form-item>
          <el-form-item label="故障等级">
            <el-select
              style="width: 100%"
              v-model="searchParam.repairLevel"
              placeholder="请选择故障等级"
              clearable
            >
              <el-option
                v-for="item in useDictionary('EamFaultLevel')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备信息">
            <el-input
              v-model="searchParam.assetInfo"
              placeholder="请输入设备编码或名称"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="工单编号">
            <el-input
              v-model="searchParam.repairCode"
              placeholder="请输入工单编号"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="报修时间">
            <el-date-picker
              v-model="searchParam.reportQueryTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getreportTime"
            />
          </el-form-item>
          <el-form-item label="维修时间">
            <el-date-picker
              v-model="searchParam.repairQueryTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getrepairTime"
            />
          </el-form-item>
        </div>
        <!-- <el-form-item label="报修人">
          <el-input
            v-model="searchParam.repairUser"
            placeholder="请输入报修人"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
          <el-button type="primary" @click="toggleSearchArea" link>
            {{ isExpanded ? "收起" : "展开" }}
            <el-icon v-if="isExpanded" class="el-icon--right">
              <i-ep-arrowUp color="#409EFC" />
            </el-icon>
            <el-icon v-else class="el-icon--right">
              <i-ep-arrowDown color="#409EFC" />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <!-- <el-button
          type="primary"
          :disabled="ids.length === 0"
          @click="handleResponse()"
        >
          <i-ep-promotion />
          应答
        </el-button> -->
        <el-button
          @click="
            useDownload(
              '/andon/andcall/callInformExport',
              '故障维修工单',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        :cell-style="{ 'text-align': 'center' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="维修单编号"
          prop="repairCode"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column label="处理状态" prop="repairStatus" min-width="120">
          <template #default="scope">
            <el-tag type="warning" v-if="scope.row.repairStatus == 0">
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
            <el-tag
              v-if="
                scope.row.repairStatus * 1 > 0 && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
            <el-tag v-if="scope.row.repairStatus * 1 >= 4" type="info">
              {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="设备名称"
          prop="assetName"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备编码"
          prop="assetCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column label="故障状态" prop="faultStatus" min-width="100">
          <template #default="scope">
            {{ useDictionary("EamFaultStatus", scope.row.faultStatus) }}
          </template>
        </el-table-column>
        <el-table-column label="故障等级" prop="repairLevel" min-width="100">
          <template #default="scope">
            <el-tag
              type="danger"
              v-if="
                scope.row.repairLevel == '1' && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
            <el-tag type="info" v-if="scope.row.repairStatus * 1 >= 4">
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
            <el-tag
              v-if="
                scope.row.repairLevel !== '1' && scope.row.repairStatus * 1 < 4
              "
            >
              {{ useDictionary("EamFaultLevel", scope.row.repairLevel) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="故障描述"
          prop="remarks"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="使用部门"
          prop="usingDepartmentName"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备位置"
          prop="locationName"
          min-width="160"
          show-overflow-tooltip
        />

        <el-table-column
          label="报修时间"
          prop="repairTime"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="故障时间"
          prop="timeOfFailure"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="报修人"
          prop="repairUserName"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column label="故障来源" prop="repairSource" min-width="100">
          <template #default="scope">
            {{ useDictionary("EamFaultSource", scope.row.repairSource) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否委外"
          prop="outSourcing"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.outSourcing === 1"
              >是</el-tag
            >
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="维修班组" prop="repairSource" min-width="150">
          <template #default="scope">
            {{ scope.row.repairTeamCode }} - {{ scope.row.repairTeamName }}
          </template>
        </el-table-column> -->
        <el-table-column
          label="维修班组"
          prop="repairTeamName"
          min-width="150"
          show-overflow-tooltip
        />
        <!-- <el-table-column label="维修人" prop="repairTeamUser" min-width="150">
          <template #default="scope">
            {{ scope.row.repairTeamUserCode }} - {{ scope.row.repairTeamUserName }}
          </template>
        </el-table-column> -->
        <el-table-column
          label="维修人"
          prop="repairTeamUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="维修开始时间"
          prop="repairStartTime"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="维修结束时间"
          prop="repairEndTime"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="维修其他发现"
          prop="repairSituation"
          min-width="180"
          show-overflow-tooltip
        />
        <!-- <el-table-column
          label="维修人"
          prop="repairTeamUser"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="应答时间"
          prop="answerTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="到达时间"
          prop="repairStartTime"
          min-width="100"
          show-overflow-tooltip
        /> -->

        <!-- <el-table-column
          label="故障模式"
          prop="faultMode"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="故障现象"
          prop="faultPhenomenon"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="故障原因"
          prop="faultReason"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="故障部位"
          prop="faultPosition"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="处理措施"
          prop="faultHandleMeasure"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <!-- <el-table-column label="维修状态" prop="repairStatus" min-width="100">
          <template #default="scope">
            {{ useDictionary("EamRepairStatus", scope.row.repairStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否委外"
          prop="outSourcing"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否停用"
          prop="enabled"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <el-table-column fixed="right" label="操作" width="170">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              :disabled="
                scope.row.repairStatus != 2 && scope.row.repairStatus != 1
              "
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              维修
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <eam-repair-handle-me-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :assetKind="assetKind"
      @reset-table="(id:string) => (id?.length ? getTableList() : search())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { getCrewListApi } from "@/utils/commonData";

defineOptions({
  name: "EAMEQUEAMREPAIRHANDLEBYME",
  inheritAttrs: false,
});

const assetKind = 1;
// 请求参数
const requestApi = "/eam/eamrepairorder/queryMyRepairOrder";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  repairStatus: "",
  repairUser: "",
  faultStatus: "",
  repairLevel: "",
  repairSource: "",
  assetInfo: "",
  repairCode: "",
  reportQueryTimeSearch: [],
  repairQueryTimeSearch: [],
  reportQueryStartTime: "",
  reportQueryEndTime: "",
  repairQueryStartTime: "",
  repairQueryEndTime: "",
  assetKind,
};

const deleteParam = {
  api: "/eam/eamrepairorder/delete",
  data: { assetKind },
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

// 切换搜索折叠
const isExpanded = ref(false);
function toggleSearchArea() {
  isExpanded.value = !isExpanded.value;
}

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

/** 打开维修单表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "维修单处理";
    addDialog.data = {
      id: roleObj.id,
    };
    addDialog.visible = true;
  } else {
    addDialog.title = "新增维修单";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开维修单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "维修单详情";
  addDialog.data = {
    id: roleObj.id,
  };
  addDialog.visible = true;
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getreportTime(data: any) {
  if (data) {
    searchParam.value.reportQueryStartTime = data[0];
    searchParam.value.reportQueryEndTime = data[1];
  } else {
    searchParam.value.reportQueryStartTime = "";
    searchParam.value.reportQueryEndTime = "";
  }
}
function getrepairTime(data: any) {
  if (data) {
    searchParam.value.repairQueryStartTime = data[0];
    searchParam.value.repairQueryEndTime = data[1];
  } else {
    searchParam.value.repairQueryStartTime = "";
    searchParam.value.repairQueryEndTime = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/eam/eamrepairorder/deleteItem/${id}`,
      method: "post",
      data: {
        assetKind,
      },
    });
    search();
  });
}

/** 选择组织机构并赋值id  start **/
function getSelectIds(ids: [] | string) {
  if (ids) {
    searchParam.value.usingDepartmentId = ids;
  } else {
    searchParam.value.usingDepartmentId = "";
  }
}

/**
 * 应答
 */
const handleResponse = (dictId?: number, title?: string) => {
  const dictIds = [dictId || ids.value].join(",");
  ElMessageBox.confirm(title ? title : "确认是否应答已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const dictIdsArr = dictIds.split(",");
    const data = {
      ids: dictIdsArr,
    };
    await request({
      url: "",
      method: "post",
      data: data,
    })
      .then(() => {
        ElMessage.success("应答成功");
        getTableList();
      })
      .finally(() => {});
  });
};

//班组
const crewList = ref(<any>[]);
async function getCrews() {
  crewList.value = (await getCrewListApi({ teamType: "1" })) || [];
}

// 使用部门选择
const dataObject = (item: string) => {
  return {
    name: item,
    defaultProps: {
      children: "nodes",
      label: "organizationName",
      value: "id",
    },
    api: "/system/sysOrganization/getUsableTree",
  };
};
onMounted(() => {
  getCrews();
});
</script>
